<?php

namespace app\admin\controller;

use app\model\PayRecord;
use app\model\RefundRecord;
use think\facade\Request;
use app\admin\service\RecordService;
use app\common\Common;
use app\model\Money as MoneyModel;
use think\response\Json;

// 记录管理
class Record extends Base
{
    private $service;

    public function __construct(RecordService $service)
    {
        parent::__construct();
        $this->service = $service;
    }

    // 渲染充值记录页面
    public function rechargeIndex()
    {
        return view();
    }

    // 渲染消费记录页面
    public function consumeIndex()
    {
        return view();
    }

    // 渲染观看记录页面
    public function watchIndex()
    {
        return view();
    }

    // 充值记录列表
    public function rechargeList()
    {
        $params = Request::param();

        $data = $this->service->getRechargeList($params);

        return Common::success($data);
    }

    // 消费记录列表
    public function consumeList()
    {
        $params = Request::param();

        $data = $this->service->getConsumeList($params);

        return Common::success($data);
    }

    // 观看记录列表
    public function watchList()
    {
        $params = Request::param();

        $data = $this->service->getWatchList($params);

        return Common::success($data);
    }

    // 导出
    public function leadingOut()
    {
        $params = Request::param();
        $message = $this->service->leadingOutRecharge($params);
        return Common::success();
    }

    // 批量退款
    public function batchRefund()
    {
        $params = Request::param();
        $data = $this->service->batchPayRefund($params,$this->getUserInfo());
        if (!empty($data['message'])) {
            return Common::fail($data['message']);
        }
        return Common::success();
    }

    // 退款列表
    public function refundList()
    {
        $params = Request::param();
        $data = $this->service->getRefundList($params);
        return Common::success($data);
    }

    // 渲染退款列表页面
    public function refundIndex()
    {
        return view();
    }

    // 渲染退款查询页面
    public function refundInfo()
    {
        return view();
    }

    // 退款查询
    public function refundQuery()
    {
        $params = Request::param();

        $data = $this->service->refundQueryDetail($params);
        if (!empty($data['message'])) {
            return Common::fail($data['message']);
        }
        return Common::success($data);
    }

    // 支付商户下拉列表
    public function payMerchantSelect()
    {
        $data = collect(PayRecord::PAY_MERCHANT_ARR)->each(function ($item,$key) {
            return [
                'pay_merchant' => $key,
                'pay_merchant_name' => $item,
            ];
        })->values()->toArray();
        return Common::success($data);
    }

    // 产品来源下拉
    public function getSourceList()
    {
        $data = collect(PayRecord::SOURCE_ARR)->each(function ($item,$key) {
            return [
                'id' => $key,
                'name' => $item,
            ];
        })->values()->toArray();
        return Common::success($data);
    }

    // 退款页面
    public function refund()
    {
        $params = Request::param();
        $record = PayRecord::where('id',$params['id'])->field('type')->findOrFail();
        switch ($record->type){
            case MoneyModel::TYPE_COMMON:
            case MoneyModel::TYPE_ACTIVITY:
            {
                $view = 'common_refund';
            }
            break;
            case MoneyModel::TYPE_UNLOCK_ALL:
            {
                $view = 'unlock_all_refund';
            }
            break;
            default:
                $view = 'vip_refund';
                break;
        }
        return view($view);
    }

    //选择退款类型
    public function selectRefundType()
    {
        $data = collect(RefundRecord::TYPE_ARR)->each(function ($value,$key){
           return ['key' => $key, 'value' => $value];
        })->values()->toArray();
        return Common::success($data);
    }

    /**
     * 订单退款
     * @return Json
     */
    public function orderRefund()
    {
        $params = Request::param();
        flog([
            'message' => '订单退款请求数据',
            'params' => $params,
        ],'order_refund');
        $data = $this->service->doRefund($params,$this->getUserInfo());
        if (!empty($data['message'])) {
            flog([
                'message' => $data['message'],
                'params' => $params,
            ],'order_refund','error');
            return Common::fail($data['message']);
        }
        return Common::success($data);
    }

    /**
     * 订单详细
     * @return Json
     */
    public function orderDetail()
    {
        $params = Request::param();
        $orderDetail = $this->service->orderDetail($params);
        return Common::success($orderDetail);
    }

    /**
     * 获取退款金额
     * @return Json
     */
    public function refundMoney()
    {
        $params = Request::param();
        $refundMoney = $this->service->countRefundMoney($params);
        return Common::success(['refund_money' => $refundMoney]);
    }

    // 退款记录导出
    public function refundLeadingOut()
    {
        $params = Request::param();
        $message = $this->service->refundLeadingOutRecharge($params);
        return Common::success();
    }
}